<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 定时任务
 */
class Crontab extends Api {

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    //取消订单 库存回滚 积分回滚 优惠券回滚
    public function task1() {

        $order_list = Db::name('order')
                ->where(['status' => 0, 'type' => 1, 'is_deleted' => 0])
                ->select();

        if ($order_list) {
            foreach ($order_list as $val) {
                $cretetime = strtotime($val['createtime']);

                $endtime = $cretetime + (5 * 60) + 3; //5分钟 加3秒

                if (time() > $endtime) {//超时未支付
                    Db::startTrans();
                    try {
                        $user_id = $val['user_id'];
                        //订单状态修改
                        $orderData['updatetime'] = date('Y-m-d H:i:s', time());
                        $orderData['status'] = 20;
                        Db::name('order')->where(['order_id' => $val['order_id']])->update($orderData);

                        //积分回滚
                        $discount_score_num = $val['discount_score_num'];

                        $user_info = Db::name('users')->where(['user_id' => $user_id])->find();
                        if ($discount_score_num > 0) {//积分回滚
                            Db::name('users')->where(['user_id' => $user_id])->setInc('score', $discount_score_num);

                            $score_log['change_amount'] = $discount_score_num;
                            $score_log['change_matters'] = 3;
                            $score_log['change_type'] = 0;
                            $score_log['user_id'] = $user_id;
                            $score_log['order_id'] = $val['order_id'];
                            $score_log['balance'] = $user_info['score'] + $discount_score_num;
                            $score_log['remark'] = '订单取消退回';

                            Db::name('score_log')->insert($score_log);
                        }

                        //优惠券回滚

                        db('user_coupon')->where('user_coupon_id', $val['coupon_id'])->update(['status' => 0]);

                        $user_box_id = $val['user_box_id'];
                        $order_goods = Db::name('order_goods')->where(['order_id' => $val['order_id']])->select();

                        //添加库存变动流水
                        $stock_log['change_type'] = 0;
                        $stock_log['change_matters'] = 4;
                        $stock_log['order_id'] = $val['order_id'];

                        //库存回滚
                        foreach ($order_goods as $goods) {
                            if ($goods['box_goods_id'] > 0 && $goods['is_callback'] > 0) {
                                Db::name('user_box_goods')->where(['box_goods_id' => $goods['box_goods_id'], 'user_box_id' => $user_box_id])->setInc('stock_num', $goods['is_callback']);
                                
                                $user_box_goods = db('user_box_goods')->where(['box_goods_id' => $goods['box_goods_id'], 'user_box_id' => $user_box_id])->find();
                                
                                $stock_log['box_goods_id'] = $goods['box_goods_id'];
                                $stock_log['goods_id'] = $goods['goods_id'];
                                $stock_log['change_amount'] = $goods['is_callback'];
                                $stock_log['quantity'] = $user_box_goods['stock_num'];
                                db('stock_log')->insert($stock_log);
                            }
                        }

                        Db::commit();
                    } catch (Exception $ex) {
                        Db::rollback();
                    }
                }
                continue;
            }
        }
    }

    public function updateAd() {
        db('ad')
                ->where('status', 0)
                ->where('starttime', '<=', date('Y-m-d H:i:s'))
                ->update(['status' => 1]);


        db('ad')
                ->where('status', 1)
                ->where('endtime', '<=', date('Y-m-d H:i:s'))
                ->update(['status' => 10]);
    }

    public function updateCoupon() {
        db('coupon')
                ->where('status', 0)
                ->where('starttime', '<=', date('Y-m-d H:i:s'))
                ->update(['status' => 1]);


        db('coupon')
                ->where('status', 1)
                ->where('endtime', '<=', date('Y-m-d H:i:s'))
                ->update(['status' => 10]);

        db('user_coupon')
                ->where('status', 0)
                ->where('expiry_end', '<=', date('Y-m-d H:i:s'))
                ->update(['status' => 10]);
    }

}
